import com from "./common";
import $ from "jquery";
let pageDeal = {

    pageSize:1,
    goahead:1,//用于判断是否需要继续加载
    startscroll:function(obj){
        let range = 40; //距下边界长度/单位px
      pageDeal.searchObj = obj;
        let totalheight = 0;
        $(window).scroll(function(){
            let srollPos = $(window).scrollTop(); //滚动条距顶部距离(页面超出窗口的高度)
            totalheight = parseFloat($(window).height()) + parseFloat(srollPos);
            /*ajax返回值data示例[offset:10,goahead:0]*/

            // 当页面滑到底部时
            if(($(document).height()-range) <= totalheight && pageDeal.goahead) {
                $("body").append('<div class="dropload-down"><div class="dropload-load"><span class="loading">数据加载中,请稍后</span></div></div>');


              pageDeal.scrollGetData(pageDeal.searchObj,pageDeal.pageSize);
                pageDeal.pageSize ++;
            }else{
            }
        });
    },
    getData:function(obj){
        com.ajax({
            url:obj.url,
            data:obj.data,
            ok:function(dat){
                obj.callback(dat);
            }
        })
    },
    scrollGetData:function(obj,pageSize){
        pageDeal.goahead = 0;
        com.ajax({
            url:obj.url,
            data:{
                Status:obj.Status,
                Page:pageSize,
                PageNum:10,
            },
            ok:function(dat){
                obj.callback(dat);
                pageDeal.goahead = 1;
                if(dat.data.length < 10){
                    // 当数据已经全部获取时,不再继续滚动
                    pageDeal.goahead = 0;
                    $(".dropload-down").html('<div class="dropload-load"><span class="loading">数据已全部加载完成</span></div>');
                    setTimeout(function(){
                        $('.dropload-down').remove();
                    },2000)
                }
            },
        })
    }
}

export default pageDeal